package com.pignest.hotel.ui.reception;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.LivingCustom;
import com.pignest.hotel.beans.LivingSpecifics;
import com.pignest.hotel.definition.OperateState;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class ModLivingWindow extends Window implements EventListener {

	private static final long serialVersionUID = 73787294711158418L;

	private Listbox teamlivingList;

	private Living curLiving;

	private Label roomName;

	private Textbox customName;

	private Textbox price;

	private Textbox certificateNO;
	private Listbox gender;
	private Listbox passportType;

	private Datebox leaveDate;

	private Textbox phone;
	private Textbox remark;
	
	private Listbox CustonListbox;
	
	private Listbox specificesList;
	
	protected boolean success;

	@Override
	public void onEvent(Event arg0) throws Exception {
		if (Events.ON_CREATE.equals(arg0.getName())) {
			onCreate();
		}
	}

	public ModLivingWindow() {
		addEventListener(Events.ON_CREATE, this);
	}

	private void onCreate() {
		teamlivingList = (Listbox) getFellow("teamlivingList");
		roomName = (Label) getFellow("roomName");
		customName = (Textbox) getFellow("customName");
		price = (Textbox) getFellow("price");
		certificateNO = (Textbox) getFellow("certificateNO");
		gender = (Listbox) getFellow("gender");
		passportType = (Listbox) getFellow("passportType");
		leaveDate=(Datebox) getFellow("leaveDate");
		phone=(Textbox)getFellow("phone");
		remark=(Textbox)getFellow("remark");
		CustonListbox=(Listbox)getFellow("CustonListbox");
		
		specificesList=(Listbox)getFellow("specificesList");
		init();
	}

	private void init() {
		queryTeamLivng();
		queryCustorm(curLiving);
		querySepc(curLiving);
	}

	public void queryTeamLivng() {
		if (this.curLiving == null) {
			return;
		}
		String serialNumber = curLiving.getSerialNumber();
		List<Living> list = null;
		if (serialNumber != null)
			list = Services.getReceptionService().queryTeamLiving(serialNumber,null);
		else
			list = Services.getReceptionService().queryTeamLiving(null,curLiving.getId());
		if (list == null)
			return;
		teamlivingList.getItems().clear();
		int index = 1;
		for (final Living l : list) {

			Listitem listitem = new Listitem();
			listitem.setValue(l.getId());
			listitem.setParent(teamlivingList);
			listitem.appendChild(new Listcell(String.valueOf(index)));
			listitem.appendChild(new Listcell(l.getRoom().getName()));
			listitem.appendChild(new Listcell(l.getRoom().getType().getName()));
			listitem.appendChild(new Listcell(Utils.toPrice(l.getRoomPrice())));
			listitem.appendChild(new Listcell(Utils.toPrice(l.getAdjustPrice())));
			listitem.appendChild(new Listcell(l.getCustormName()));
			listitem.appendChild(new Listcell(l.getMaster2()));
            listitem.addEventListener(Events.ON_CLICK, new EventListener() {
				
				@Override
				public void onEvent(Event event) throws Exception {
					// TODO Auto-generated method stub
					selectLivingContent(l);
					queryCustorm(l);
					querySepc(l);
				}
			});
			index++;
		}
	}
    public void selectLivingContent(Living l)
    {
    	String sRoomName =l.getRoom().getName();
    	String sName=l.getCustormName();
    	String sPrice =Utils.toPrice(l.getAdjustPrice());
    	String sCno =l.getPassportNumber();
    	String sPtype =l.getPassportType();
    	String sSex =l.getSex();
    	roomName.setValue(sRoomName);
    	customName.setValue(sName);
    	price.setValue(sPrice);
    	certificateNO.setValue(sCno);
    	UIUtils.setSelectedItem(passportType, sPtype);
    	UIUtils.setSelectedItem(gender, sSex);
    	leaveDate.setValue(l.getExpectLeaveDate());
    	phone.setValue(l.getPhone());
    	remark.setValue(l.getRemark());
    }
    public void queryCustorm(Living l)
    {
    	CustonListbox.getItems().clear();
    	List<LivingCustom> lcs =Services.getReceptionService().queryCustoms(l);
    	if(lcs==null)
    		return ;
    	for(LivingCustom lc:lcs)
    	{
    		Listitem listitem = new Listitem();
			 listitem.setValue(lc.getId());
			 listitem.setParent(CustonListbox);
			 listitem.appendChild(new Listcell(""));
			 listitem.appendChild(new Listcell(lc.getName()));
			 listitem.appendChild(new Listcell(lc.getSex()));
			 listitem.appendChild(new Listcell(lc.getIshost()));
			 listitem.appendChild(new Listcell(lc.getCertificatetype()));
			 listitem.appendChild(new Listcell(lc.getCertificateno()));
			 listitem.appendChild(new Listcell(lc.getPhone()));
			 if(lc.getIshost().equals("是")){
				 listitem.setCheckable(false);
				 listitem.setStyle("background-color:#dddddd;");
			 }
    	}
    }
    public void querySepc(Living l){
    	specificesList.getItems().clear();
        if(l==null)
        	return ;
    	StringBuffer hql=new StringBuffer();
		hql.append(" from LivingSpecifics where living.id='"+l.getId()+"'");
		List <LivingSpecifics> list=Services.getReceptionService().find(hql.toString());
		if(list==null)
			return ;
		for( LivingSpecifics s:list){
		
			Listitem listitem = new Listitem();
			listitem.setParent(specificesList);
			listitem.appendChild(new Listcell(s.getRoom()));//房间名
			listitem.appendChild(new Listcell(s.getOperateType()));//操作类型
			listitem.appendChild(new Listcell(Utils.toPrice(s.getRoomPrice())));//房价
			listitem.appendChild(new Listcell(Utils.toPrice(s.getPay())));//金额
			//listitem.appendChild(new Listcell(DateUtil.toString(s.getGrantDate())));//授权时间"
			listitem.appendChild(new Listcell(DateUtil.toString(s.getOperateDate())));//操作时间
			listitem.appendChild(new Listcell(s.getOpUser()));//操作时间
			listitem.appendChild(new Listcell(s.getRemark()));//备注
		}
    }
    public void saveInfo()
    {
    	try{
    	String selectId =(String)UIUtils.getSelectedValue(teamlivingList);
    	if(selectId==null){
    		UIUtils.alert("请选择一入住!");
    		return ;
    	}
    	Living l =Services.getReceptionService().get(Living.class, selectId);
    	if(l==null){
    		UIUtils.alert("无效入住!");
    		return ;
    	}
    	String sName=customName.getValue();
    	int sPrice =Utils.toPrice(price.getValue());
    	String sCno =certificateNO.getValue();
    	String sPtype=UIUtils.getSelectedValue(passportType);
    	String sSex =UIUtils.getSelectedValue(gender);
    	Date sLeaveDate=leaveDate.getValue();
    	String sPhone=phone.getValue();
    	String sRemark=remark.getValue();
    	if(sName==null||sName.isEmpty()){
    		UIUtils.alert("客人名字不能为空!");
    		return ;
    	}
    	if(sCno==null||sCno.isEmpty()){
    		UIUtils.alert("证件号不能为空!");
    		return ;
    	}
    	if (!Utils.checkPrice(price.getValue())) {
    		UIUtils.alert("请输入调整价, 免费则请输入0");
			return;
		}
    	LivingSpecifics cs= null;
    	if(sPrice!=l.getAdjustPrice()){
    		cs=addSpecifices(l);
    		cs.setRoomPrice(l.getAdjustPrice());
    		cs.setPay(sPrice);
    		cs.setOperateType(OperateState.ROOMPRICE.getName());
    		cs.setRemark("调整房原房价："+Utils.toPrice(l.getAdjustPrice())+"现房价:"+Utils.toPrice(sPrice));
    	}
    	l.setCustormName(sName);
    	l.setAdjustPrice(sPrice);
    	l.setSex(sSex);
    	l.setPassportType(sPtype);
    	l.setExpectLeaveDate(sLeaveDate);
    	l.setPhone(sPhone);
    	l.setRemark(sRemark);
    	l.setPassportNumber(sCno);
    	l.setTeamState(1);
    	LivingCustom lc=null;
    	if(!Services.getReceptionService().isExistCurCustom(l, sCno)){
    		//新增
    	    lc = new LivingCustom();
    		lc.setCertificateno(sCno);
    		lc.setCertificatetype(sPtype);
    		lc.setIshost("否");
    		lc.setLiving(l);
    		lc.setName(sName);
    		lc.setPhone(sPhone);
    		lc.setSex(sSex);
    	}else
    	{
    	   lc =Services.getReceptionService().queryLivingCustom(l,sCno);
    		lc.setCertificateno(sCno);
    		lc.setCertificatetype(sPtype);
    		lc.setIshost("否");
    		lc.setLiving(l);
    		lc.setName(sName);
    		lc.setPhone(sPhone);
    		lc.setSex(sSex);
    		
    	}
    	
    	
    	Services.getReceptionService().updateLiving(l, lc,cs);
    	//queryCustorm(l);
    	init();
    	//UIUtils.alert("操作成功!");
    	success=true;
    	}catch(Exception e){
    		e.printStackTrace();
    		UIUtils.alert("操作失败!"+e.getMessage());
    	}
    }
    public void delete(){
    	try{
	    	String cusid =(String)UIUtils.getSelectedValue(CustonListbox);
	    	String selectId =(String)UIUtils.getSelectedValue(teamlivingList);
	    	Living l =Services.getReceptionService().get(Living.class, selectId);
	    	if(UIUtils.confirm("你确定要删除吗?")){
	        	Services.getReceptionService().delete(LivingCustom.class, cusid);
	        	UIUtils.alert("操作成功!");
	    	}
	    	queryCustorm(l);
	    	
    	}catch(Exception e){
    		e.printStackTrace();
    		UIUtils.alert("删除成功!");
    	}
    }
    public void setTeamLeader()
    {
    	try{
    	String selectId =(String)UIUtils.getSelectedValue(teamlivingList);
    	if(selectId==null){
    		UIUtils.alert("请选择一入住!");
    		return ;
    	}
    	
    	Services.getReceptionService().updateTeamLeader(selectId);
    	UIUtils.alert("操作成功!");
    	init();
    	
    	}catch(Exception e){
    		e.printStackTrace();
    		UIUtils.alert(e.getMessage());
    	}
    }
    public void setHostCustorm(){
    	try{
        	String selectId =(String)UIUtils.getSelectedValue(teamlivingList);
        	String cusId =(String)UIUtils.getSelectedValue(CustonListbox);
        	if(selectId==null){
        		UIUtils.alert("请选择一入住!");
        		return ;
        	}
        	if(cusId==null){
        		UIUtils.alert("请选择一客人!");
        		return ;
        	}
        	Services.getReceptionService().updateHostCustorm(selectId,cusId);
        	success=true;
        	init();
        	
        	}catch(Exception e){
        		e.printStackTrace();
        		UIUtils.alert(e.getMessage());
        	}
    }
    public LivingSpecifics addSpecifices(Living l){
    	LivingSpecifics cs =new LivingSpecifics();
    	cs.setLiving(l);
    	cs.setRoom(l.getRoom().getName());
    	cs.setOperateDate(new Date());
    	cs.setOpUser(Hotel.getCurrentUser().getName());
    	cs.setGrantDate(l.getGrantDate());
    	return cs;
    }
	public Living getCurLiving() {
		return curLiving;
	}

	public void setCurLiving(Living curLiving) {
		this.curLiving = curLiving;
	}
	public boolean isSuccess() {
		return success;
	}
}
